<?php
/**
 * Created by PhpStorm.
 * Author: Fedax
 * Date: 2017/6/30
 * Time: 17:41
 */

namespace app\admin\controller;

use app\admin\model\Role as RoleModel;
use think\Request;

/**
 * 角色
 * Class Role
 * @package app\admin\controller
 */
class Role extends Index
{
    /**
     * 角色列表
     * @return \think\response\View
     */
    public function index()
    {
        return view();
    }

    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function indexData()
    {
        $roleModel = new RoleModel();
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('list_rows', 10),
        );
        $data = $roleModel->getDataList($page_config);
        return json($data);
    }

    /**
     * 添加
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function save(Request $request)
    {
        if(request()->isPost()){
            $roleModel = new RoleModel();
            $re = $roleModel->save(array('role_name' => $request->post('role_name')));
            if ($re) {
                $this->success(lang('add') . lang('success'));
            }
            $this->error(lang('add') . lang('false'));
        }
        return redirect('/Miss');
    }

    /**
     * 删除
     * @return \think\response\Redirect
     */
    public function delete()
    {
        if(request()->isPost()){
            $roleModel = new RoleModel();
            $re = $roleModel->destroy(input('id'));
            if ($re) {
                $this->success(lang('delete') . lang('success'));
            }
            $this->error(lang('delete') . lang('false'));
        }
        return redirect('/Miss');
    }

    /**
     * 编辑
     * @return \think\response\Redirect
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if (request()->isPost()) {
            $roleModel = new RoleModel();
            $data = $roleModel->getOrFail(input('id'));
            $data->role_name = input('role_name');
            if ($data->save()) {
                $this->success(lang('edit') . lang('success'));
            }
            $this->error(lang('edit') . lang('false'));
        }
        return redirect('/Miss');
    }
}